<?php
/**
 * 数据库操作类
 */

class DBUtil
{
    //DB服务器
    private $host = "";
    //DB端口
    private $port = "";
    //DB数据库名
    private $dbname = "";
    //DB用户名
    private $user = "";
    //DB密码
    private $password = "";
    //DB链接
    public $db_link;
    //结果集
    public $result;
    //
    public $rows;
    //记录
    public $myrow;
    //错误信息
    public $db_error = "";
    public $autocommitflag = true;
    /**
     * 构造函数
     */
    public function DBUtil(){
    	$dbConfig = [
		    'host' => 'localhost',
		    'port' => '3306',
		    'dbname' => 'eco',
		    'username' => 'root',
		    'password' => ''
		];
        $this->host = $dbConfig['host'];
        $this->port = $dbConfig['port'];
        $this->dbname = $dbConfig['dbname'];
        $this->user = $dbConfig['username'];
        $this->password = $dbConfig['password'];
        $this->db_error = "";
        $this->connect();
    }

    /**
     * 连接DB
     */
    public function connect(){
    	//DB已连接时
        if($this->db_link) {
            exit();
        }
        else {
            $this->db_link = mysql_pconnect( $this->host.":".$this->port,$this->user,$this->password);
            
			if (!$this->db_link || !mysql_select_db($this->dbname)) {
                exit();
            }
            mysql_query("SET NAMES utf8",$this->db_link);
        }
    }

    /**
     * 关闭DB连接
     */
    public function close(){
        if ($this->db_link) {
            if(!mysql_close($this->db_link)){
                //exit();
            }
        }
    }

    /**
     * 运行sql
     * @param  $sql
     * @return $this->result
     */
    public function query($sql){
    	//清空结果集
        if ($this->result) {
        	unset($this->result);
        }
        //运行sql
        $this->result = mysql_query($sql,$this->db_link);
        //运行失败时
        if (!$this->result) {
        	$message = mysql_error($this->db_link)."\n";
        	$message .= "SQL:";
        	$message .= $sql;
            exit();
        }
        return $this->result;
    }

    /**
     * 取得sql运行结果的第一条
     * @param  $sql
     * @return $this->rows
     */
    public function getOneRow($sql) {
        if ($this->rows) {
            unset($this->rows);
        }
        $this->query($sql);
        if(is_resource($this->result)){
        	//$this->rows = mysql_fetch_array($this->result);
        	$row = mysql_fetch_array($this->result);
        	$tmp = array();
        	if(is_array($row)){
	        	foreach($row as $key=>$item){
	        		if(!is_numeric($key)){
	        			$tmp[$key] = $item;
	        		}
	        	}
	        }
        	$this->rows = $tmp;
        }else{
        	$this->rows = array();
        }
        return $this->rows;
    }

    /**
     * 取得sql运行结果
     * @param  $sql
     * @return $this->myrow
     */
    public function getAll($sql,$option=null) {
        if ($this->myrow) {
        	unset($this->myrow);
        }
        $this->query($sql);
        $myrow = array();
        if(is_array($option)){
        	$myrow[] = $option;
        }
        if(is_resource($this->result)){
	        while ($row=mysql_fetch_array($this->result)) {
	        	$tmp = array();
	        	foreach($row as $key=>$item){
	        		if(!is_numeric($key)){
	        			$tmp[$key] = $item;
	        		}
	        	}
	        	$myrow[] = $tmp;
	        }
	        $this->free($this->result);
	    }
        $this->myrow = $myrow;
        return $this->myrow;
    }

    /**
     * 释放结果集
     * @param  $result
     * @param  $return boolean
     */
    public function free($result) {
        return is_resource($this->result) ? mysql_free_result($result) : true;
    }

    /**
     * 取得结果集的条数
     * @param  $result
     * @param  $return
     */
    public function num_rows ($result) {
        return mysql_num_rows($result);
    }

    /**
     * sql转义(防注入)
     * @param $str            unescaped_string
     * @param $escapedStr     escaped_string
     */
    public function escape($str)
    {
        $escapedStr = mysql_real_escape_string($str,$this->db_link);
        return $escapedStr;
    }

    /**
     * 事物autoCommit
     * @param boolean $mode
     */
    public function autoCommit($mode = false)
    {
    	$this->autocommitflag = $mode;
    	$mode = $mode ? 1 : 0;
        if(!mysql_query("set autocommit = ".$mode,$this->db_link)){
            exit();
        }
    }
	/**
     * 开启事物
     */
     public function start()
    {
    	mysql_query("BEGIN",$this->db_link);
    }
    /**
     * 回滚事物
     */
     public function rollback()
    {
    	return mysql_query("ROLLBACK",$this->db_link);
    }
    /**
     * 提交事物
     */
     public function commit()
    {
    	mysql_query("COMMIT",$this->db_link);
    }
    
    public function getlastid(){
    	return mysql_insert_id($this->db_link);
    }
}
?>